package com.zut.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.mapper.BookorderMapper;
import com.zut.mapper.SchduleMapper;
import com.zut.pojo.Bookorder;
import com.zut.pojo.Parents;
import com.zut.pojo.Schdule;
import com.zut.pojo.Teacher;
import com.zut.service.TeacherService;
import com.zut.service.impl.BookorderServiceImpl;
import com.zut.service.impl.ParentsServiceImpl;
import com.zut.service.impl.SchduleServiceImpl;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author teacher
 * @since 2021-05-31
 */
@Controller
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @Resource
    private SchduleServiceImpl schduleService;

    @Resource
    private SchduleMapper schduleMapper;

    @Resource
    private ParentsServiceImpl parentsService;
    @Resource
    private BookorderServiceImpl bookorderService;
    //按性别查询老师
    @RequestMapping("/selectTeacherSex")
    public void selectTeacherSex(String sex, Model model){
        List<Teacher> teachers = teacherService.selectTeacherSex(sex);
        model.addAttribute(teachers);
    }


    //按教龄查询老师
    @RequestMapping("/selectTeacherYear")
    public void selectTeacherYear(Integer begin, Integer end,Model model){
        List<Teacher> teachers = teacherService.selectTeacherYear(begin, end);
        model.addAttribute(teachers);
    }

    //按是否获奖查询老师
    @RequestMapping("selectTeacherIsAcmer")
    public void selectTeacherIsAcmer(Integer flag,Model model){
        List<Teacher> teachers = teacherService.selectTeacherIsAcmer(flag);
        model.addAttribute(teachers);
    }

    //按名字精确查找
    @RequestMapping("selectTeacherName")
    public void selectTeacherName(String teaName,Model model){
        List<Teacher> teachers = teacherService.selectTeacherName(teaName);
        model.addAttribute(teachers);
    }

    //按id查找老师
    @RequestMapping("/selectTeacherById/{id}")
    public String selectTeacherById(@PathVariable("id") Integer id, Model model){
        Object object[]=new Object[5];

        Teacher teacher = teacherService.selectTeacherById(id);
        List<Schdule> schduleList=schduleService.sleSch(id);
        System.out.println(schduleList);
        object[1]= teacher;
        object[2]=schduleList;
//        model.addAttribute("teacher",teacher);
////
////        model.addAttribute("schlist",schduleList);
        model.addAttribute("obj",object);
        return "teacher_details";
    }

    //添加老师,返回1添加成功
    @RequestMapping("addTeacher")
    public String addTeacher(Teacher teacher,Model model){
        boolean i = teacherService.addTeacher(teacher);
        if(i==true){
            model.addAttribute("msg","添加成功");
        }else{
            model.addAttribute("msg","添加失败");
        }
        return "back/add-teacher";
    }

    //删除老师，返回1删除成功
    @RequestMapping("/deleteTeacher")
    public void deleteTeacher(Integer teaid,Model model){
        int i = teacherService.deleteTeacher(teaid);
        if(i==1){
            model.addAttribute("msg","删除成功");
        }else{
            model.addAttribute("msg","删除失败");
        }
    }



    //展示全部老师信息
    @RequestMapping("/teacherlist")
    public String selectAllTeacher(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        List<Teacher> teachers = teacherService.selectAllTeacher();
        Page<Teacher> Tpage = new Page<>(pn,3);
        Page<Teacher> page = teacherService.page(Tpage, null);
        List<Teacher> records = page.getRecords();
        model.addAttribute("teachers",page);
        return "teacherlist";
    }

    //老师详情页
    @GetMapping("/{id}")
    public String findById(@PathVariable("id") Integer id,Model model){
        model.addAttribute("details",teacherService.getById(id));
        return "teacher_details";
    }

    //模糊查询老师信息
    @RequestMapping("/teacherlist/sea")
    public String searchtea(@RequestParam("search") String search, @RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model){
        Page<Teacher> Tpage = new Page<>(pn,3);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("teaid", search)
                .or().like("teaname",search)
                .or().like("realteaname",search)
                .or().like("teasex",search)
                .or().like("teayear",search)
                .or().like("isacmer",search)
                .or().like("teahour",search)
                .or().like("tearesume",search)
        ;
        Page<Teacher> page = teacherService.page(Tpage, queryWrapper);
       // List<Teacher> teachers = teacherService.seatea(search);
        model.addAttribute("teachers",page);
       // model.addAttribute("teachers", teachers);
        return "teacherlist";
    };

    @PostMapping(value = "/xuanke")
    @ResponseBody
    public  HashMap<String,Object>  xuanke(HttpSession session,int teaid, int position) {



        Parents parent = (Parents) session.getAttribute("loginParent");
        Parents parents = parentsService.selectParentsById(parent.getParid());
        HashMap<String,Object> map = new HashMap<>();
        if(parents.getParhour()==0) {
            map.put("code",0);
            return map;
        }
        int parid=parents.getParid();
        int x=(position+1)%7;
        int y=(position+1)/7; if(x!=0) y+=1; if(x==0) x=7;
        QueryWrapper<Schdule> wrapper=new QueryWrapper<>();
        wrapper.eq("teaid",teaid).eq("week",x).eq("day",y);
        Schdule schdule= schduleMapper.selectOne(wrapper);
        schdule.setParid(parid);
        schdule.setState2(1);
        schduleMapper.updateById(schdule);

        parents.setParhour(parents.getParhour()-1);
        parentsService.updateById(parents);

        Bookorder bookorder=new Bookorder();
        bookorder.setParid(parid); bookorder.setTeaid(teaid);
        bookorderService.save(bookorder);

        //return  "redirect:/selectTeacherById/{teaid}";

        map.put("code",1);
        return map;
    }


}

